import com.ygqh.baby.solr.YgSolrClient;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.util.NamedList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by jyx on 2018/2/7.
 */

public class MyproductSearchIndex {




    private static final String FULL_IMPORT = "FULL_IMPORT";
    private static final String DELTA_IMPORT = "DELTA_IMPORT";
    private static final SolrClient solrClient = YgSolrClient.INSTANCE.getSolrClient();

    public static void main(String[] args) {
        try {
            SolrRequest<QueryResponse> request = buildRequest(FULL_IMPORT, "product");
            request.setPath("/dataimport");
            NamedList<Object> resp = solrClient.request(request);
            System.out.println("product 重建索引成功，具体结果:" + resp.toString());
        } catch (Exception e) {
            System.out.println("product 重建索引成功，异常信息:" + e.getMessage());
            e.printStackTrace();
        }
    }



    public static SolrRequest<QueryResponse> buildRequest(String importType, String entity) {
        Map<String, String> map = new HashMap<>();
        switch (importType) {
            case DELTA_IMPORT:
                map.put("command", "delta-import");
                map.put("clean", "false");
                map.put("commit", "true");
                map.put("optimize", "false");
                map.put("index", "false");
                map.put("entity", entity);
                map.put("debug", "false");
                map.put("wt", "json");
                return new QueryRequest(new MapSolrParams(map));
            case FULL_IMPORT:
                /**
                 * command: full-import
                 * verbose: false
                 * clean: true
                 * commit: true
                 * optimize: false
                 * core: product
                 * entity: ygProduct
                 * name: dataimport
                 */
                map.put("command", "full-import");
                map.put("verbose", "false");
                map.put("clean", "true");
                map.put("commit", "true");
                map.put("optimize", "false");
                map.put("core", entity);
                map.put("name", "dataimport");
                map.put("wt", "json");
                return new QueryRequest(new MapSolrParams(map));
        }
        return null;
    }
}
